<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>学生信息管理</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      a {
        text-decoration: none;
        color: #721c24;
      }
      h1 {
        text-align: center;
        color: #333;
        margin: 20px 0;
      }
      table {
        margin: 0 auto;
        width: 800px;
        border-collapse: collapse;
        color: #004085;
      }
      th {
        padding: 10px;
        background: #cfe5ff;

        font-size: 20px;
        font-weight: 400;
      }
      td,
      th {
        border: 1px solid #b8daff;
      }
      td {
        padding: 10px;
        color: #666;
        text-align: center;
        font-size: 16px;
      }
      tbody tr {
        background: #fff;
      }
      tbody tr:hover {
        background: #e1ecf8;
      }
      .info {
        width: 900px;
        margin: 50px auto;
        text-align: center;
      }
      .info input,
      .info select {
        width: 80px;
        height: 27px;
        outline: none;
        border-radius: 5px;
        border: 1px solid #b8daff;
        padding-left: 5px;
        box-sizing: border-box;
        margin-right: 15px;
      }
      .info button {
        width: 60px;
        height: 27px;
        background-color: #004085;
        outline: none;
        border: 0;
        color: #fff;
        cursor: pointer;
        border-radius: 5px;
      }
      .info .age {
        width: 50px;
      }
    </style>
  </head>

  <body>
    <h1>新增学员</h1>
    <form class="info" autocomplete="off">
      姓名：<input type="text" class="uname" name="uname" /> 年龄：<input
        type="text"
        class="age"
        name="age"
      />
      性别:
      <select name="gender" class="gender">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
      薪资：<input type="text" class="salary" name="salary" />

      就业城市：<select name="city" class="city">
        <option value="北京">北京</option>
        <option value="上海">上海</option>
        <option value="广州">广州</option>
        <option value="深圳">深圳</option>
        <option value="曹县">曹县</option>
      </select>
      <button class="add">录入</button>
    </form>

    <h1>就业榜</h1>
    <table>
      <thead>
        <tr>
          <th>学号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>薪资</th>
          <th>就业城市</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <!-- 
        <tr>
          <td>1001</td>
          <td>欧阳霸天</td>
          <td>19</td>
          <td>男</td>
          <td>15000</td>
          <td>上海</td>
          <td>
            <a href="javascript:">删除</a>
          </td>
        </tr> 
        -->
      </tbody>
    </table>
    <script>
      // 获取数据
      const uname = document.querySelector(".uname");
      const age = document.querySelector(".age");
      const gender = document.querySelector(".gender");
      const salary = document.querySelector(".salary");
      const city = document.querySelector(".city");
      const tbody = document.querySelector("tbody");
      // 声明一个空数组 增加和删除数据都要用到
      const arr = [];
      // 录入模块
      // 表单提交事件 submit
      const info = document.querySelector(".info");
      info.addEventListener("submit", function (e) {
        // 阻止表单的默认提交行为
        e.preventDefault();
        // 创建对象
        const obj = {
          stuId: arr.length + 1,
          uname: uname.value,
          age: age.value,
          gender: gender.value,
          salary: salary.value,
          city: city.value,
        };
        arr.push(obj);
        // 清空表单 .reset() 重置
        info.reset();
        // 调用渲染函数
        render();
      });

      // 渲染函数 增加和删除都需要渲染
      // 遍历数组
      function render() {
        // 先清空tbody 以前的行 把新数组里面的数据渲染完毕
        tbody.innerHTML = "";
        for (let i = 0; i < arr.length; i++) {
          // 生成tr
          const tr = document.createElement("tr");
          tr.innerHTML = `
            <td>${arr[i].stuId}</td>
            <td>${arr[i].uname}</td>
            <td>${arr[i].age}</td>
            <td>${arr[i].gender}</td>
            <td>${arr[i].salary}</td>
            <td>${arr[i].city}</td>
            <td>
              <a href="javascript:" data-id=${i}>删除</a>
            </td>
          `;
          // 追加元素 父元素.appendChild(子元素)
          tbody.appendChild(tr);
        }
      }
      // 删除模块
      // 点击删除按钮事件
      tbody.addEventListener("click", function () {
        if (event.target.tagName === "A") {
          // 获取删除的id
          const id = event.target.dataset.id;
          // 删除数组
          arr.splice(id, 1);
          // 调用渲染函数
          render();
        }
      });
    </script>
  </body>
</html>
